package com.qf.dao.impl;

import com.qf.dao.AdminDao;
import com.qf.domain.Goods;
import com.qf.domain.User;
import com.qf.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.awt.print.Book;
import java.sql.SQLException;
import java.util.List;

/**
 * @author YDW
 * projectName:shopping
 * time:2022/4/21 11:16
 * description:
 */
public class AdminDaoImpl implements AdminDao {
    @Override
    public User selectByUsernameAndPassword(String username, String password) {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" select id,username,password,email,gender,flag,role,code from tb_user where username=? and password=md5(?) and role=0 ";
        try {
            User user = queryRunner.query(sql, new BeanHandler<>(User.class), username, password);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> findAllUser() {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" select id,username,password,email,gender,flag,role,code from tb_user where flag=1 ";
        try {
            List<User> userList = queryRunner.query(sql, new BeanListHandler<>(User.class));
            return userList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void deleteUserById(String id) {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" delete from tb_user where id=?";
        try {
            queryRunner.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<User> findByUsernameAndGender(String where, List<Object> params) {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" select id,username,password,email,gender,flag,role,code from tb_user "+where+" and flag=1";
        try {
            List<User> userList=queryRunner.query(sql,new BeanListHandler<>(User.class),params.toArray());
            return userList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> findAllinvalidUser() {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" select id,username,password,email,gender,flag,role,code from tb_user where flag=2 ";
        try {
            List<User> userList = queryRunner.query(sql, new BeanListHandler<>(User.class));
            return userList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void loseUser(String id) {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" update tb_user set flag=2 where id=? ";
        try {
            queryRunner.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<User> findinvalidUserByUsernameAndGender(String where, List<Object> params) {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" select id,username,password,email,gender,flag,role,code from tb_user "+where+" and flag=2";
        try {
            List<User> userList=queryRunner.query(sql,new BeanListHandler<>(User.class),params.toArray());
            return userList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void activateUser(String id) {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" update tb_user set flag=1 where id=? ";
        try {
            queryRunner.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Long findUserCount() {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" select count(0) from tb_user";
        try {
            Long UserCount = (Long) queryRunner.query(sql, new ScalarHandler());
            return UserCount;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return Long.valueOf(0);
    }

    @Override
    public List<User> findData(int startindex, int pageSize) {
        QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
        String sql=" select id,username,password,email,gender,flag,role,code from tb_user limit ?,? ";
        try {
            List<User> data=queryRunner.query(sql, new BeanListHandler<>(User.class),startindex,pageSize);
            return data;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
